package cst.neu.neufooddemo.activity;

import android.content.Intent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import cst.neu.neufooddemo.R;
import cst.neu.neufooddemo.beans.Result;
import cst.neu.neufooddemo.listener.RetrofitListener;
import cst.neu.neufooddemo.model.UserModel;

public class RegisterActivity extends BaseActivity implements RetrofitListener<Result> {
    private EditText uName,uPass,ruPass,address,tel,bZ;
    private Button btn_reg;
    @Override
    void initData() {

    }
    void initViews() {
        setLayout(R.layout.activity_register);
        uName= (EditText) findViewById(R.id.editText_name_re);
        uPass= (EditText) findViewById(R.id.editText_pass_re);
        ruPass= (EditText) findViewById(R.id.editText_pass_rer);
        tel= (EditText) findViewById(R.id.editText_phone_re);
        bZ= (EditText) findViewById(R.id.editText_beizhu);
        address= (EditText) findViewById(R.id.editText_address_re);
        btn_reg= (Button) findViewById(R.id.button_register);
    }

    @Override
    void initEvents() {
        btn_reg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(uPass.getText().toString().equals(ruPass.getText().toString())){
                    register();
                }else {
                    showToast("请确认两次密码相同");
                }
            }
        });
    }

    public void register(){
        UserModel model=new UserModel();
        model.register(uName.getText().toString(),uPass.getText().toString(),address.getText().toString(),tel.getText().toString(),bZ.getText().toString(),this);
    }

    @Override
    public void onSuccess(Result result, int flag) {
        if(result.getSuccess().equals("1")){
            Intent intent=new Intent();
            intent.putExtra("user",uName.getText().toString());
            setResult(RESULT_OK,intent);
            finish();
            showToast("注册成功");
        }
        else {
            showToast("注册失败");
        }
    }

    @Override
    public void onFail() {

        showToast("网络发生错误");
    }
}
